package com.zero.manage.web.components.controller;

import com.zero.manage.web.components.dto.LoginDto;
import com.zero.manage.web.components.entity.rbac.SysUserEntity;
import com.zero.manage.web.components.query.SysOnLineUserQuery;
import com.zero.manage.web.components.query.SysUserQuery;
import com.zero.manage.web.components.service.SysUserService;
import io.gitee.zerowsh.enums.ToolEnums;
import io.gitee.zerowsh.util.LogTag;
import io.gitee.zerowsh.util.PageResult;
import io.gitee.zerowsh.util.PublicResult;
import io.gitee.zerowsh.validation.group.Insert;
import io.gitee.zerowsh.validation.group.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/sysUser")
@Api(tags = "系统用户")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("sysLogin")
    @ApiOperation(value = "登录", notes = "系统登录接口")
    @LogTag
    public PublicResult login(@RequestBody @Validated LoginDto loginDto, HttpServletResponse response) {
        return sysUserService.login(loginDto, response);
    }

    @GetMapping("/captcha")
    @ApiOperation(value = "验证码", notes = "生成验证码")
    public PublicResult captcha() {
        return sysUserService.captcha();
    }

    @PostMapping("sysLogout")
    @ApiOperation(value = "登出", notes = "系统登出接口")
    @LogTag
    public PublicResult login() {
        return sysUserService.logout();
    }

    @GetMapping("initFrontEndInfo")
    @ApiOperation(value = "初始化前端信息", notes = "查询用户拥有的菜单、权限、路由")
    public PublicResult initFrontEndInfo() {
        return sysUserService.initFrontEndInfo();
    }

    @GetMapping("updateState")
    @ApiOperation(value = "修改用户状态", notes = "修改用户状态")
    public PublicResult updateState(@RequestParam String id, @RequestParam ToolEnums.StateEnums state) {
        return sysUserService.updateState(id, state);
    }

    @GetMapping("resetPassword")
    @ApiOperation(value = "重置密码", notes = "重置用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户编码", dataType = "String"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "String")
    })
    public PublicResult resetPassword(@RequestParam String userId, @RequestParam String newPassword) {
        return sysUserService.resetPassword(userId, newPassword);
    }

    @GetMapping("updatePassword")
    @ApiOperation(value = "修改密码", notes = "修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "老密码", dataType = "String"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "String")
    })
    public PublicResult updatePassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        return sysUserService.updatePassword(oldPassword, newPassword);
    }

    @PostMapping("onlineUserPage")
    @ApiOperation(value = "在线用户", notes = "获取在线用户")
    public PageResult<List<SysUserEntity>> onlineUserPage(@RequestBody SysOnLineUserQuery query) {
        return sysUserService.onlineUserPage(query);
    }

    @GetMapping("forcedOffline")
    @ApiOperation(value = "下线用户", notes = "强制下线用户")
    public PublicResult forcedOffline(@RequestParam String name) {
        return sysUserService.forcedOffline(name);
    }

    @PostMapping("import")
    @ApiOperation(value = "导入", notes = "导入用户数据")
    public PublicResult importInfo(@RequestParam MultipartFile file) throws IOException {
        return sysUserService.importInfo(file);
    }

    @GetMapping("export")
    @ApiOperation(value = "导出", notes = "导出用户数据")
    public void exportInfo(HttpServletResponse response, SysUserQuery query) throws IOException {
        sysUserService.exportInfo(response, query);
    }

    @PostMapping("pageList")
    @ApiOperation(value = "分页列表", notes = "查询分页数据集合")
    public PageResult<List<SysUserEntity>> pageList(@RequestBody @Validated SysUserQuery query) {
        return sysUserService.pageList(query);
    }

    @PostMapping("del")
    @ApiOperation(value = "删除", notes = "公用删除接口")
    @LogTag
    public PublicResult deleteByIds(@RequestBody List<SysUserEntity> es) {
        return sysUserService.deleteByIds(es);
    }

    @PostMapping("add")
    @ApiOperation(value = "新增", notes = "公用新增接口")
    @LogTag
    public PublicResult save(@RequestBody @Validated({Insert.class}) SysUserEntity entity) {
        return sysUserService.add(entity);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改", notes = "公用修改接口")
    @LogTag
    public PublicResult update(@RequestBody @Validated({Update.class}) SysUserEntity entity) {
        return sysUserService.update(entity);
    }
}
